package com.zeng.vueserve.coreservice.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.vueserve.coreservice.mapper.UserRoleMapper;
import com.zeng.vueserve.coreservice.entity.UserRole;
import com.zeng.vueserve.coreapi.vo.UserRoleVo;
import com.zeng.vueserve.coreapi.service.UserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public UserRoleVo addOne(UserRoleVo userRoleVo) {
    UserRole userRole = UserRole.builder().build();
    BeanUtils.copyProperties(userRoleVo, userRole, IgnoreStringUtil.ignoreExclude());
    int insert = userRoleMapper.insert(userRole);
    return BeanUtil.beanToVo(userRole, userRoleVo);
    }

    @Override
    public UserRoleVo getOneByCode(String code) {

    UserRole userRole = this.userRoleMapper.selectById(code);
    UserRoleVo userRoleVo = BeanUtil.beanToVo(userRole, new UserRoleVo());

    return userRoleVo;
    }

    @Override
    public Integer update(UserRoleVo userRoleVo) {
    UserRole userRole = BeanUtil.VoToBean(userRoleVo,
    new UserRole(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.userRoleMapper.updateById(userRole);
    }

    @Override
    public Integer delete(String code) {
    return this.userRoleMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.userRoleMapper.deleteBatchIds(codes);
    }

    @Override
    public List<UserRoleVo> findPage(Page page, UserRoleVo userRoleVo) {
        UserRole userRole = BeanUtil.VoToBean(userRoleVo, new UserRole());
        this.userRoleMapper.selectPage(page, this.getWrapper(userRoleVo));
        List orders = page.getRecords();
        List<UserRoleVo> userRoleVos = new ArrayList<>();
        orders.forEach(e->{
            userRoleVos.add(BeanUtil.beanToVo(e, new UserRoleVo()));
        });
        page.setRecords(null);
        return userRoleVos;
        }


    @Override
    public Integer count(UserRoleVo userRoleVo) {
        Integer integer = this.userRoleMapper.selectCount(this.getWrapper(userRoleVo));
        return integer;
    }

    public AbstractWrapper getWrapper(UserRoleVo userRoleVo) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        if (userRoleVo != null) {


        }
        return wrapper;
    }
}
